<?php
class tplModel extends RPHP_Model
{

    protected $_auth = null;

    //检查权限
    public function checkPriv($pv, $return = true, $menu = true)
    {
        $auth = $this->auth->CheckPriv($pv, $return, $menu);
        if ($return) {
            $this->_auth = $auth;
        }

        return $auth;
    }

    //get document
    public function getDocs($docid)
    {
        if (is_array($docid)) {
            $where = " WHERE `docid` IN (" . implode(",", array_unique($docid)) . ")";
        } else {
            $where = " WHERE `docid`=" . intval($docid);
        }

        $sql = "SELECT `docid`,`filename`,`create_time` FROM {$this->_prefix['primary']}documents " . $where;
        $rs  = $this->sql($sql);
        if ($rs) {
            foreach ($rs as $r) {
                $doc[$r['docid']] = $this->c['doc_host'] . date("Ymd", $r['create_time']) . "/" . date("H", $r['create_time']) . "/" . date("i", $r['create_time']) . "/" . $r['filename'];

            }
            return $doc;
        } else {
            return false;
        }

    }

    //save document
    public function saveDocument($name, $filename, $filesize, $format, $create_time)
    {
        $sql = "INSERT INTO {$this->_prefix['primary']}documents
		(`shopid`,`name`,`filename`,`filesize`,`format`,`create_time`) VALUES
		(" . $this->_auth['shopid'] . ",'" . $name . "','" . $filename . "'," . $filesize . ",'" . $format . "'," . $create_time . ")";

        $docid = $this->sql($sql);
        return $docid;
    }

    //获取首页版块
    public function getHomeTpl($htid = 0)
    {

        $tpl[-1] = array(
            'htid'   => -1,
            'title'  => '首页顶部滚动广告',
            'stat'   => 1,
            'static' => 1,
        );
        $tpl[-2] = array(
            'htid'   => -2,
            'title'  => '首页六个固定广告',
            'stat'   => 1,
            'static' => 1,
        );
        $tpl[-3] = array(
            'htid'   => -3,
            'title'  => '商品列表顶部广告',
            'stat'   => 1,
            'static' => 1,
        );
        $tpl[-4] = array(
            'htid'   => -4,
            'title'  => '悦纷享顶部广告',
            'stat'   => 1,
            'static' => 1,
        );

        if ($htid > 0) {
            $tsql = " AND `htid`=" . $htid;
        } else {
            if ($htid < 0) {
                return $tpl[$htid];
            }

        }

        $sql = "SELECT `htid`,`title`,`title_tiny`,`stat` FROM {$this->_prefix['primary']}home_tpl WHERE `shopid`=" . $this->_auth['shopid'] . $tsql . " AND `stat`<3 ORDER BY `sort` ASC,`htid` ASC";

        if ($htid > 0) {
            $data = $this->db->getrow($sql);
        } else {

            $new = $tpl;

            $data = $this->sql($sql);

            $data = array_merge($new, $data);
        }

        return $data;
    }

    //获取首页广告
    public function getHomeAds($htid = 0)
    {

        if ($htid > 0) {
            $tsql = " AND `htid`=" . $htid;
        }

        $sql  = "SELECT `adid`,`title`,`htid`,`stat` FROM {$this->_prefix['primary']}home_ads WHERE `shopid`=" . $this->_auth['shopid'] . $tsql . " AND `stat`<3 ORDER BY `sort` ASC,`htid` ASC,`adid` ASC";
        $data = $this->sql($sql);
        if ($data) {

            foreach ($data as $r) {
                $new[$r['htid']][$r['adid']] = $r;
            }

            return $new;
        } else {
            return false;
        }

    }

    //保存版块排序
    public function saveHomeTplSort($da)
    {
        $code = 900;
        if ($da['value'] != "") {
            $ids = json_decode($da['value']);
            foreach ($ids as $k => $r) {
                $sql = "UPDATE {$this->_prefix['primary']}home_tpl SET `sort`=" . $k . " WHERE `htid`=" . intval($r->id) . " AND `shopid`=" . $this->_auth['shopid'];
                $this->sql($sql);
            }
            $code = 200;
        }
        return $code;
    }

    //保存版块
    public function saveHomeTpl($da, $htid = 0)
    {

        $code       = 900;
        $title      = addslashes(htmlspecialchars(trim($da['title'])));
        $title_tiny = addslashes(htmlspecialchars(trim($da['title_tiny'])));
        $stat       = intval($da['stat']);

        //修改
        if ($htid > 0) {

            $rs = $this->getHomeTpl($htid);

            if ($rs) {

                $sql = "UPDATE {$this->_prefix['primary']}home_tpl SET `stat`=" . $stat . ",`title`='" . $title . "',`title_tiny`='" . $title_tiny . "',`last_staffid`=" . $this->_auth['staffid'] . ",`last_staffedit`=" . $_SERVER['REQUEST_TIME'] . " WHERE `htid`=" . $rs['htid'];
                $rd  = $this->sql($sql);
                if ($rd > 0) {
                    $code = 200;
                } else {
                    $code = 403;
                }

            } else {
                $code = 404;
            }

        }
        //新增
        else {
            if ($da['title'] != "") {

                $sql = "INSERT INTO {$this->_prefix['primary']}home_tpl
					(`shopid`,`title`,`title_tiny`,`stat`,`last_staffid`,`last_staffedit`) VALUES
					(" . $this->_auth['shopid'] . ",'" . $title . "','" . $title_tiny . "'," . $stat . "," . $this->_auth['staffid'] . "," . $_SERVER['REQUEST_TIME'] . ")";

                $rd = $this->sql($sql);

                if ($rd > 0) {
                    $code = 200;
                } else {
                    $code = 403;
                }

            }
        }
        return $code;

    }

    //删除版块
    public function deleteHomeTpl($htid)
    {
        $code = 404;
        $data = $this->getHomeTpl($htid);
        if ($data['htid']) {

            $sql = "UPDATE {$this->_prefix['primary']}home_tpl SET `stat`=3 WHERE `htid`=" . $data['htid'] . " AND `shopid`=" . $this->_auth['shopid'];
            $rc  = $this->sql($sql);
            if ($rc > 0) {
                $code = 200;
            } else {
                $code = 403;
            }

        }
        return $code;

    }

    //删除广告
    public function deleteHomeAd($adid)
    {
        $code = 404;
        $data = $this->getSingleHomeAd($adid);
        if ($data['adid']) {

            $sql = "UPDATE {$this->_prefix['primary']}home_ads SET `stat`=3 WHERE `adid`=" . $data['adid'] . " AND `shopid`=" . $this->_auth['shopid'];
            $rc  = $this->sql($sql);
            if ($rc > 0) {
                $code = 200;
            } else {
                $code = 403;
            }

        }
        return $code;

    }

    //获取单个首页广告
    public function getSingleHomeAd($adid = 0)
    {

        $sql = "SELECT * FROM {$this->_prefix['primary']}home_ads WHERE `adid`=" . intval($adid) . " AND `shopid`=" . $this->_auth['shopid'] . " AND `stat`<3";

        return $this->db->getrow($sql);

    }

    //保存首页广告
    public function saveHomeAd($da, $file, $adid = 0)
    {
        $title      = addslashes(htmlspecialchars(trim($da['title'])));
        $url        = addslashes(trim($da['url']));
        $pic_url    = addslashes(trim($da['pic_url']));
        $stat       = intval($da['stat']);
        $docid      = 0;
        $htid       = intval($da['htid']);
        $savehomead = true;
        $code       = 403;

        //大图处理
        if ($file['pic_big']['tmp_name'] != "" && $file['pic_big']['size'] > 0) {
            $savehomead = false;
            if (in_array($file['pic_big']['type'], $this->c['upload']['mime'])) {

                if ($file['pic_big']['size'] <= $this->c['upload']['size']) {
                    $format = $this->upload->rf($file['pic_big']['name']);

                    if (in_array($format, $this->c['upload']['fm'])) {

                        //创建目录
                        $dir['date'] = $this->c['upload']['dir'] . date("Ymd", $_SERVER['REQUEST_TIME']);
                        if (!file_exists($dir['date'])) {
                            mkdir($dir['date']);
                        }

                        $dir['hour'] = $dir['date'] . DIRECTORY_SEPARATOR . date("H", $_SERVER['REQUEST_TIME']);
                        if (!file_exists($dir['hour'])) {
                            mkdir($dir['hour']);
                        }

                        $dir['minute'] = $dir['hour'] . DIRECTORY_SEPARATOR . date("i", $_SERVER['REQUEST_TIME']);
                        if (!file_exists($dir['minute'])) {
                            mkdir($dir['minute']);
                        }

                        if (file_exists($dir['minute'])) {

                            //移动文件
                            $filename = md5($file['pic_big']['tmp_name'] . $_SERVER['REQUEST_TIME']) . "." . $format;
                            $filepath = $dir['minute'] . DIRECTORY_SEPARATOR . $filename;

                            if (move_uploaded_file($file['pic_big']['tmp_name'], $filepath)) {

                                $docid = $this->saveDocument($file['pic_big']['name'], $filename, $file['pic_big']['size'], $format, $_SERVER['REQUEST_TIME']);

                                if ($docid > 0) {
                                    $savehomead = true;
                                } else {
                                    $code = 701;
                                    @unlink($filepath);
                                }
                            } else {
                                $code = 705;
                            }

                        } else {
                            $code = 706;
                        }

                    } else {
                        $code = 703;
                    }

                } else {
                    $code = 704;
                }

            } else {
                $code = 703;
            }

        }

        if ($savehomead) {
            //修改广告
            if ($adid > 0) {

                $data = $this->getSingleHomeAd($adid);
                if ($data) {

                    if ($docid > 0) {
                        $data['docid'] = $docid;
                    }

                    $sql = "UPDATE {$this->_prefix['primary']}home_ads SET `title`='" . $title . "',`url`='" . $url . "',`pic_url`='" . $pic_url . "',`docid`=" . $data['docid'] . ",`stat`=" . $stat . ",`last_staffid`='" . $this->_auth['staffid'] . "',`last_staffedit`=" . $_SERVER['REQUEST_TIME'] . " WHERE `adid`=" . $data['adid'];
                    $rc  = $this->sql($sql);
                    if ($rc > 0) {
                        $code = 200;
                    } else {
                        $code = 403;
                    }

                } else {
                    $code = 404;
                }

            }
            //新增
            else {
                $sql = "INSERT INTO {$this->_prefix['primary']}home_ads (`shopid`,`htid`,`title`,`url`,`docid`,`pic_url`,`stat`,`last_staffid`,`last_staffedit`) VALUES (" . $this->_auth['shopid'] . "," . $htid . ",'" . $title . "','" . $url . "'," . $docid . ",'" . $pic_url . "'," . $stat . "," . $this->_auth['staffid'] . "," . $_SERVER['REQUEST_TIME'] . ")";

                $newid = $this->sql($sql);
                if ($newid > 0) {

                    $code = 200;

                }
            }

        }

        return $code;
    }

    //保存广告排序
    public function saveHomeAdSort($da)
    {
        $code = 900;
        if ($da['value'] != "") {
            $ids = json_decode($da['value']);
            foreach ($ids as $k => $r) {
                $sql = "UPDATE {$this->_prefix['primary']}home_ads SET `sort`=" . $k . " WHERE `adid`=" . intval($r->id) . " AND `shopid`=" . $this->_auth['shopid'];
                $this->sql($sql);
            }
            $code = 200;
        }
        return $code;
    }

}
